﻿using System.IO;
using System.Web;
using OpenWaves.Web;
using OpenWaves;

[assembly: PublicAssemblyResources("OpenWaves.Web.Controls.UploadHandler.ashx")]

namespace OpenWaves.Web.Controls
{
    public class UploadHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var fileName = context.Request["name"] ?? string.Empty;

            if (fileName.IsNullOrWhiteSpace())
            {
                context.Response.StatusCode = 406;
                context.Response.Write("missing data");
                return;
            }

            var fileUpload = context.Request.Files[0];

            var tempFileName = Path.GetTempFileName();
            using (var fs = new FileStream(tempFileName, FileMode.Create, FileAccess.Write))
            {
                fs.Append(fileUpload.InputStream);
            }

            context.Response.ContentType = ContentTypes.Text.Plain;
            context.Response.Write(tempFileName);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}